﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing.Printing;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Xml.Linq;
using YunShop_WebAPI.BLL;
using YunShop_WebAPI.Model;

namespace YunShop_WebAPI.API.Controllers
{
    /// <summary>
    /// 天润定制
    /// </summary>
    public class TianRunController : ApiController
    {
        /// <summary>
        /// 1.1、商品分类列表,shop_appid=at7FbVyk8zNJCWZZ/szBfIHjj/tpPyK8Jcf4fzQH4vI=
        /// </summary>
        [HttpPost, HttpGet]
        [ActionName("GetAdvertiseList")]
        public Result_Model GetAdvertiseList()
        {
            //string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            string shop_appid = "PvMgVl8C9gF8PkZh1IrYfx9hWHfuo0S9eGSTipA33js=";
            int datatype = CommonFun.ConvertToInt(HttpContext.Current.Request["type"]);
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string msql = @"SELECT  a.FItemID, a.FName,c.FHostUrl + a.FImgUrl as FImgUrl, a.FUrlType,
                        case a.FUrlType when 0 then a.FGoodsID else a.FGoodsClsID end as FUrlID
                        FROM dbo.t_Advertise AS a INNER JOIN
	                        dbo.t_Corpration AS b ON a.FCorpID = b.FItemID INNER JOIN
	                        dbo.t_BaseHost AS c ON b.FHostID = c.FHostID
                        WHERE (a.FCorpID = @mCorpID) AND (a.FSendFlag = 1) AND (a.FDataType = @mDataType)
                        ORDER BY a.FOrder";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@mCorpID", SqlDbType.BigInt, mCorpID);
            par_list.Add("@mDataType", SqlDbType.Int, datatype);
            ArrayList data = DBHelper.Select(msql, par_list.Get_List(), DBHelper.ConnMaster);
            return new Result_Model(1, "获取成功", data);
        }

        /// <summary>
        /// 1.2、商品列表,shop_appid=at7FbVyk8zNJCWZZ/szBfIHjj/tpPyK8Jcf4fzQH4vI=
        /// </summary>
        [HttpPost, HttpGet]
        [ActionName("GetGoodsList")]
        public Result_Model GetGoodsList()
        {
            //string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            string shop_appid = "PvMgVl8C9gF8PkZh1IrYfx9hWHfuo0S9eGSTipA33js=";
            int mUrlID = CommonFun.ConvertToInt(HttpContext.Current.Request["FUrlID"]);
            int mUrlType = CommonFun.ConvertToInt(HttpContext.Current.Request["FUrlType"]);
            string sName = CommonFun.ConvertToString(HttpContext.Current.Request["FName"]);
            int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            string msql = @"SELECT a.FItemID, a.FName, dbo.t_GoodsDetail.FProImage as FQrCode,a.FSpec,a.FUnitName, 
                Convert(float,a.FPosPrice) as FPrice,c.FHostUrl+ dbo.t_GoodsDetail.FGoodImg as FGoodImg, c.FHostUrl 
                FROM dbo.t_Goods AS a INNER JOIN
                dbo.t_Corpration AS b ON a.FCorpID = b.FItemID INNER JOIN
                dbo.t_BaseHost AS c ON b.FHostID = c.FHostID INNER JOIN
                dbo.t_GoodsDetail ON a.FItemID = dbo.t_GoodsDetail.FItemID
                WHERE (a.FCorpID = @mCorpID) and (a.FDeleted = 1) ";
            SqlParameterList par_list = new SqlParameterList();
            par_list.Add("@mCorpID", SqlDbType.BigInt, mCorpID);   
            
            if (mUrlType == 0)
            {
                par_list.Add("@mGoodsID", SqlDbType.Int, mUrlID);
                msql = msql + " AND (a.FItemID = @mGoodsID)";
            }
            else
            {
                if (string.IsNullOrEmpty(sName))
                {
                    par_list.Add("@mClassID", SqlDbType.Int, mUrlID);
                    msql = msql + " AND (a.FClassID = @mClassID)";
                }
                else
                {
                    par_list.Add("@mName", SqlDbType.VarChar, "%" + sName + "%");
                    msql = msql + " AND (a.FName like @mName)";
                }
            }

            ArrayList data = DBHelper.Select(msql, par_list.Get_List(), DBHelper.ConnMaster);

            foreach (Hashtable row in data)
            {
                string[] sFProImages = CommonFun.ConvertToString(row["FQrCode"]).Split('$');
                string sQrCode = "";
                if (sFProImages.Count() > 0) sQrCode = CommonFun.ConvertToString(row["FHostUrl"]) + sFProImages[1];
                row["FQrCode"] = sQrCode;
            }

            return new Result_Model(1, "获取成功", data);
        }
    }
}